package com.ssy.lingxi.settle.accounts.serviceimpl;

import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.dto.model.constant.ApplyAmountRowBillTypeEnum;
import com.ssy.lingxi.settle.accounts.entity.BusinessReconciliationRowDO;
import com.ssy.lingxi.settle.accounts.model.constant.SettleAccountsServiceContants;
import com.ssy.lingxi.settle.accounts.model.vo.request.ReconciliationDetailVO;
import com.ssy.lingxi.settle.accounts.model.vo.response.ToBeSubmitReconciliationRowVO;
import com.ssy.lingxi.settle.accounts.repository.BusinessReconciliationRowRepository;
import com.ssy.lingxi.settle.accounts.service.IBusinessReconciliationRowService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 业务对账单行明细信息接口实现
 * @author Saske
 * @date 2021/12/9
 * @version 2.0.0
 */
@Service
public class BusinessReconciliationRowServiceImpl implements IBusinessReconciliationRowService {
    
    @Resource
    private BusinessReconciliationRowRepository rowRepository;

    /**
     * 对账单明细行信息
     * @param reconciliationDetailVO 对账单详情查询条件
     * @return 对账单明细行信息
     */
    @Override
    public Wrapper<PageData<ToBeSubmitReconciliationRowVO>> findDetails(UserLoginCacheDTO sysUser, ReconciliationDetailVO reconciliationDetailVO) {

        Pageable pageable = PageRequest.of(reconciliationDetailVO.getCurrent() -1, reconciliationDetailVO.getPageSize(), Sort.by("id").descending());
        Specification<BusinessReconciliationRowDO> specification = (root, query, criteriaBuilder) -> criteriaBuilder.equal(root.get("reconciliationNo").as(String.class), reconciliationDetailVO.getReconciliationNo());

        Page<BusinessReconciliationRowDO> list = rowRepository.findAll(specification, pageable);

        List<ToBeSubmitReconciliationRowVO> data = list.stream().map(map ->{
            ToBeSubmitReconciliationRowVO row = new ToBeSubmitReconciliationRowVO();
            row.setOrderId(map.getOrderId());
            row.setId(map.getId());
            row.setSourceContractId(map.getSourceContractId());
            row.setOrderNo(map.getOrderNo());
            row.setProductId(map.getSettlementProductId());
            row.setExpectPayTime(map.getSettlementDate().format(SettleAccountsServiceContants.DEFAULT_DATE_FORMATTER));
            row.setDeliveryBatch(map.getDeliveryBatch());
            row.setDeliveryNo(map.getDeliveryNo());
            row.setReceiveNo(map.getReceiveNo());
            row.setProductNo(map.getProductNo());
            row.setProductName(map.getName());
            row.setSpec(map.getSpec());
            row.setCategory(map.getCategory());
            row.setBrand(map.getBrand());
            row.setUnit(map.getUnit());
            row.setTaxRate(map.getTaxRate());
            row.setBillAmount(map.getBillAmount());
            row.setPrice(map.getPrice());
            row.setReconciliationQuantity(map.getReconciliationQuantity());
            row.setReconciliationMoneyAmount(map.getReconciliationMoneyAmount());
            row.setCurrentReconciliationQuantity(map.getCurrentQuantity());
            row.setCurrentMoney(map.getCurrentMoney());
            row.setOrderStatus(map.getOrderStatus());
            row.setOrderType(map.getOrderType());
            // 单据类型
            row.setBillType(map.getBillType());
            row.setBillTypeName(ApplyAmountRowBillTypeEnum.getNameByCode(map.getBillType()));
            // 单据日期
            String billDate = map.getBillDate().format(SettleAccountsServiceContants.DEFAULT_DATE_FORMATTER);
            row.setBillDate(billDate);
            return row;
        }).collect(Collectors.toList());

        return Wrapper.success(new PageData<>(list.getTotalElements(),data));
    }
}
